package hongcaosp.app.android.video.online.holder;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import hongcaosp.app.android.R;
import hongcaosp.app.android.common.BaseAdapter;
import hongcaosp.app.android.common.BaseHolder;
import hongcaosp.app.android.video.online.OnLineCategoryActivity;
import hongcaosp.app.android.video.online.bean.OnlineMusic;
import hongcaosp.app.android.video.online.bean.OnlineRank;

public class OnlineRankHolder extends BaseHolder<OnlineRank> {

    private TextView tv_type,tv_all;
    private RecyclerView rv_content;

    private OnlineMusicHolder.IOnItemClickListener onItemClickListener;

    private Activity activity;

    public OnlineRankHolder(View itemView,Activity activity,OnlineMusicHolder.IOnItemClickListener onItemClickListener) {
        super(itemView);
        this.onItemClickListener = onItemClickListener;
        this.activity = activity;
        tv_type = itemView.findViewById(R.id.tv_type);
        tv_all = itemView.findViewById(R.id.tv_all);
        rv_content = itemView.findViewById(R.id.rv_content);

        tv_all.setOnClickListener(allClickListener);
    }

    @Override
    public void setData(OnlineRank onlineRank) {
        super.setData(onlineRank);
        tv_type.setText(onlineRank.getTagName());

        BaseAdapter<OnlineMusic> adapter;
        rv_content.setLayoutManager(new LinearLayoutManager(rv_content.getContext()));
        rv_content.setAdapter(adapter = new BaseAdapter<OnlineMusic>() {
            @NonNull
            @Override
            public BaseHolder<OnlineMusic> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new OnlineMusicHolder(LayoutInflater.from(rv_content.getContext()).inflate(R.layout.item_online_music,parent,false),
                        onItemClickListener);
            }
        });
        adapter.setData(onlineRank.getMusic());
        adapter.notifyDataSetChanged();
    }

    private View.OnClickListener allClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent1 = new Intent(activity,OnLineCategoryActivity.class);
            intent1.putExtra("id",getData().getId());
            intent1.putExtra("title",getData().getTagName());
            activity.startActivityForResult(intent1,999);
        }
    };
}
